<?php

abstract class dl_connect {
    private $host = __HOST;
    private $user = __USER_NAME;
    private $password = __PASSWORD;
    private $dbName = __DB_NAME;
    protected $con; //bien connect
    private $res;
    private $rows;
    
    /**
     * contructor 
     **/
    public function __construct(){
                     ;
    }
    
    protected  function Connect(){
        $this->con = mysql_connect($this->host,$this->user,$this->password) or die("khong the ket noi");
        mysql_select_db($this->dbName);
        mysql_query("SET NAMES 'utf8'"); //set ti?ng vi?t
       
    }
  
    //***********************************************************************//
    // PUBLIC FUNCTION
    //**********************************************************************// 
    /**
    * Query table
    * @param $tableName: ten cua bang can truy van
    * @param $condition: dieu kien cua de lay gia tri trong bang 
    * @return resource Query
    * */
    public function ExecuteTable($tableName, $Condition=null){
        if($Condition !=null){
            $sql = "SELECT * FROM {$tableName} WHERE {$Condition}";
        }else{
            $sql = "SELECT * FROM {$tableName} ";    
        }          
        mysql_query("SET NAMES 'utf8'"); //set ti?ng vi?t        
        $this->res =  mysql_query($sql) or die("loi truy van");         
        return  $this->res;
    }
    public function ExecuteTable2($tableName, $Condition=null){
        if($Condition !=null){
            $sql = "SELECT Max(ID) FROM {$tableName} WHERE {$Condition}";
        }else{
            $sql = "SELECT Max(ID) FROM {$tableName} ";    
        }          
        mysql_query("SET NAMES 'utf8'"); //set ti?ng vi?t        
        $this->res =  mysql_query($sql) or die("loi truy van");         
        return  $this->res;
    }
    /**
    * Query sql
    * @param $sql:cau lenh sql
    * @return Resoure Query
    * */
    function ExecuteSQL($sql){
    	mysql_query("SET NAMES 'utf8'"); //set ti?ng vi?t
        $this->res = mysql_query($sql,$this->con);     
        return $this->res ;
    }  
    
    /**
    * Show message 
    * dung javascript
    * @return void
    * */
    public function ShowMessage($message){
        echo '<script type="text/javascript">
            alert("'.$message.'");
        </script>';
    }
    
    /**
     * 
     * Di chuyen toi trang $page
     * @param unknown_type $page: trang can di chuyen toi
     */
    function loctionPage($page) {
    	echo '<script type="text/javascript">
			    window.location = "'.$page.'"; 
			</script>';
    }
    
    /**
     * 
     * Get all info cua table
     * @param unknown_type $tableName
     * @return Array
     */
    function getAll($tableName, $condition=null) {
    	if($condition!=null){
    		$this->ExecuteTable($tableName,$condition);
    		$arr = mysql_fetch_assoc($this->res);
    	}else{
    		$this->ExecuteTable($tableName);
    		$arr =array();
    		while ($this->rows = mysql_fetch_assoc($this->res)){
    		  $arr[]=$this->rows;
    		}	
    	}  
    	return $arr;
    }
       
    function __destruct() {
    //	mysql_close($this->con);
    	//mysqli_free_result($this->res);
    	//mysqli_free_result($this->rows);	
    }
}

    
?>